﻿using Game.NetWork;
using GameEntitySystem;

namespace Game
{
    public class ComponentTaskPackage : IPackage
    {
        public enum EventType
        {
            SyncStat,
            TurnToLowSen,
            TurnToHighSen,
            ZeroSen
        }

        public bool Lordtask1Net;

        public bool BloodPoolNet;

        public bool Dodo1Net;

        public bool CityNet;

        public bool YSNet;

        public int MainTaskIndexNet;

        public bool MainTaskConditionsNet;

        public EventType m_eventType;

        public ushort entityId;

        public byte ID => 112;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public ComponentTaskPackage()
        {
        }

        public ComponentTaskPackage(ComponentTask flu, EventType eventType)
        {
            entityId = flu.Entity.EntityId;
            m_eventType = eventType;
            Lordtask1Net = flu.Lordtask1;
            Dodo1Net = flu.Dodo1;
            MainTaskIndexNet = flu.MainTaskIndex;
            MainTaskConditionsNet = flu.MainTaskConditions;
            BloodPoolNet = flu.BloodPool;
            CityNet = flu.City1;
            YSNet = flu.YS;
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.Write(entityId);
            writer.WriteEnum(m_eventType);
            writer.Write(MainTaskIndexNet);
            writer.Write(MainTaskConditionsNet);
            writer.Write(BloodPoolNet);
            writer.Write(Dodo1Net);
            writer.Write(Lordtask1Net);
            writer.Write(CityNet);
            writer.Write(YSNet);
        }

        public void ReadData(PackageStreamReader reader)
        {
            entityId = reader.ReadUInt16();
            m_eventType = reader.ReadEnum<EventType>();
            MainTaskIndexNet = reader.ReadInt32();
            MainTaskConditionsNet = reader.ReadBoolean();
            BloodPoolNet = reader.ReadBoolean();
            Dodo1Net = reader.ReadBoolean();
            Lordtask1Net = reader.ReadBoolean();
            CityNet = reader.ReadBoolean();
            YSNet = reader.ReadBoolean();
        }

        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            if (m_eventType != 0)
            {
                return;
            }

            projectNet.FindEntityById(entityId, delegate (Entity entity)
            {
                ComponentTask componentTask = entity.FindComponent<ComponentTask>();
                if (componentTask != null)
                {
                    componentTask.MainTaskIndex = MainTaskIndexNet;
                    componentTask.MainTaskConditions = MainTaskConditionsNet;
                    componentTask.Lordtask1 = Lordtask1Net;
                    componentTask.Dodo1 = Dodo1Net;
                    componentTask.BloodPool = BloodPoolNet;
                    componentTask.City1 = CityNet;
                    componentTask.YS = YSNet;
                }
            });
        }
    }
}